<html>

<head>
    <style>
        .flip-container {
            perspective: 1000;
        }
        /* .flip-container:hover .flipper,
        .flip-container.hover .flipper {
            transform: rotateY(180deg);
        } */
        .flip-container,
        .front,
        .back {
            width: 320px;
            height: 480px;
        }
        .flipper {
            transition: 0.6s;
            transform-style: preserve-3d;
            position: relative;
        }
        .front,
        .back {
            backface-visibility: hidden;
            position: absolute;
            top: 0;
            left: 0;
        }
        .front {
            z-index: 2; 
        }
        .back {
            transform: rotateY(180deg);
        }
        .flip-container:hover .flipper,
        .flip-container.hover .flipper,
        .flip-container.flip .flipper {
        transform: rotateY(180deg);
        }
    </style>
</head>

<body>
    <div class="flip-container" ontouchstart="this.classList.toggle('hover');">
        <div id="mydiv">
            <div class="front">
                <img src="img/1.jpg" alt="" width="320px" height="320px">
            </div>
            <div class="back">
                <img src="img/2.jpg" alt="" width="320px" height="320px">
            </div>
        </div>
    </div>
    <script>
        document.querySelector('#mydiv').classList.toggle('flipper');
    </script>
</body>

</html>